package cn.soft863.filter.head;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.soft863.users.pojo.AUsers;

/**
 * 用户登陆验证的Filter
 * @author lenovo-yz
 *
 */
public class UsersAuthFilter implements Filter
{

	@Override
	public void destroy()
	{
		System.out.println("=====AuthFilter====destroy=");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException
	{
		/* 拦截
		 * 
		 * ~拦截哪些路径或者信息
		 * ~放行
		 * ~拦截
		 *  */
		HttpServletRequest request = (HttpServletRequest) req ; 
		HttpServletResponse response = (HttpServletResponse) res ; 
		
		/* session */
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		if(users != null)
		{
			/* 肯定走登陆了 */
			/* 放行 */
			chain.doFilter(request, response);
		}else
		{
			/* 客户端跳转到登陆页面 */
			String info = "小伙,别想通过我访问登陆后的页面" ;
			/* 放到session */
			session.setAttribute("info", info);
			response.sendRedirect(request.getContextPath() + "/NoLoginServlet?method=usersLogin");
		}
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
		System.out.println("=====AuthFilter====init=");
	}

}
